feat(util): introduce a "fused" body combinator#145
Merged
Conversation
4c77dab to
02e11f5
Compare
this commit introduces a new `Body` combinator to the `http-body-util` library, `http_body_util::combinators::Fuse<B>`. this combinator is roughly equivalent to the `std::iter::Fuse<I>` iterator, which returns `None` after the inner iterator returns it once. while bodies *should* return `Poll::Ready(None)` indefinitely after reaching the end of the stream or returning an error, this combinator can help prevent further polling of an underlying body implementation, in the same manner that `std::iter::Iterator::fuse()` helps prevent an underlying iterator that might e.g. yield `Some(value)` after yielding `None`, or panic. Signed-off-by: katelyn martin <me+cratelyn@katelyn.world>
cratelyn
commented
Apr 2, 2025
Comment on lines
+70
to
+79
| fn is_end_stream(&self) -> bool { | ||
| self.inner.is_none() | ||
| } | ||
|
|
||
| fn size_hint(&self) -> SizeHint { | ||
| self.inner | ||
| .as_ref() | ||
| .map(B::size_hint) | ||
| .unwrap_or_else(|| SizeHint::with_exact(0)) | ||
| } |
Member
Author
There was a problem hiding this comment.
i found myself wishing for this recently, since this pull request was opened.
besides the panic avoidance that this middleware can offer in certain situations, should the inner body not accept repeated polling after yielding a Ready(None) or Ready(Some(Err(_))), this middleware offers a nice additional benefit: it provides an accurate is_end_stream() for inner bodies that use the default implementation of this trait method, which always returns false.
seanmonstar
approved these changes
Apr 8, 2025
Member
seanmonstar
left a comment
There was a problem hiding this comment.
Looks excellent to me! Thanks for the thorough docs and tests too! <3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
this commit introduces a new
Bodycombinator to thehttp-body-utillibrary,http_body_util::combinators::Fuse<B>.this combinator is roughly equivalent to the
std::iter::Fuse<I>iterator, which returnsNoneafter the inner iterator returns it once.while bodies should return
Poll::Ready(None)indefinitely after reaching the end of the stream or returning an error, this combinator can help prevent further polling of an underlying body implementation, in the same manner thatstd::iter::Iterator::fuse()helps prevent an underlying iterator that might e.g. yieldSome(value)after yieldingNone, or panic.